//
//  MyCustomTableViewCell.m
//  防MVVM
//
//  Created by lanou on 16/4/28.
//  Copyright © 2016年 范明星. All rights reserved.
//

#import "MyCustomTableViewCell.h"
#define ImageToLeft 10
#define IndicatorToRight 10
#define LabelToImage 5
#define DetailViewToIndicator 15
#define KSCreenWidth [UIScreen mainScreen].bounds.size.width

#import "MyCellItemModel.h"
#import "UIView+XMGExtension.h"
@interface MyCustomTableViewCell ()

@property (nonatomic, strong) UIImageView *headImage;
@property (nonatomic, strong) UIImageView *indicatorImage;
@property (nonatomic, strong) UIImageView *rightImage;
@property (nonatomic, strong) UISwitch *aswitch;
@property (nonatomic, strong) UILabel *itemLbel;
@property (nonatomic, strong) UILabel *detaillabel;

@end


@implementation MyCustomTableViewCell


- (void)setItem:(MyCellItemModel *)item{
    _item = item;
    //设置UI
    [self setupUI];
}

- (void) setupUI {
    //selector  方法选择器
    //SEL实际上就是c语言字符串， 是@selector类型
    //1.清空
    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    //2.布局, 都要先判断有没有这个东西
    if (self.item.itemImage)
    {
        //设置最左边的图片
        [self setupImg];
    }

    if (self.item.itemName)
    {
        //设置左边的Label
        [self setLeftlabel];
        
    }
    
    
    
        //设置cell类型
    switch (self.item.type) {
            case MyCellItemTypeSystem:
                [self setupMyCellItemTypeSystem];
                
                break;
             case MyCellItemTypeSwitch:
                [self setupMyCellItemTypeSwitch];
                break;
            default:
                break;
        }
        
   
    
    if (self.item.itemRightImage)
    {
        //设置右边的图片
        [self setRightImage];
    }
    
    if (self.item.itemRightname)
    {
        //设置右边的Item
        [self setRightItem];
    }
    
    //设置分割线
    UIView *line = [[UIView alloc] init];
    line.frame = CGRectMake(10, self.contentView.height - 1, KSCreenWidth - 20, 1);
    line.backgroundColor = [UIColor blackColor];
    line.alpha = .2f;// = 0.05
    [self.contentView addSubview:line];
    
}

//设置左图片
- (void)setupImg
{
    self.headImage = [[UIImageView alloc] initWithImage:self.item.itemImage];
    self.headImage.x = ImageToLeft;
    self.headImage.centerY = self.contentView.centerY;
    [self.contentView addSubview:self.headImage];
}

//设置左label
- (void)setLeftlabel
{
    self.itemLbel = [[UILabel alloc] init];
    self.itemLbel.text = self.item.itemName;
    self.itemLbel.font = [UIFont systemFontOfSize:14];
    self.itemLbel.size =  [self.itemLbel.text boundingRectWithSize:CGSizeMake(FLT_MAX, FLT_MAX) options:0 attributes:@{NSFontAttributeName :[UIFont systemFontOfSize:14]} context:nil].size;
    self.itemLbel.centerY = self.contentView.centerY;
    self.itemLbel.x = CGRectGetMaxX(self.headImage.frame) + LabelToImage;
    [self.contentView addSubview:self.itemLbel];
}



- (UIImageView *)indicatorImage{
    if (!_indicatorImage) {
        _indicatorImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon-arrow1"]];
        self.indicatorImage.centerY = self.contentView.centerY;
        self.indicatorImage.x = KSCreenWidth - self.indicatorImage.width - IndicatorToRight;
        
    }
    return _indicatorImage;
}
//设置系统cell样式
- (void)setupMyCellItemTypeSystem
{
    [self.contentView addSubview:self.indicatorImage];
}







//开关的懒加载
- (UISwitch *)aswitch{
    if (!_aswitch) {
        _aswitch = [[UISwitch alloc] init];
        self.aswitch.centerY = self.contentView.centerY;
        self.aswitch.x = KSCreenWidth - self.aswitch.width - IndicatorToRight;
        [self.aswitch addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
    }
    return _aswitch;
}

- (void)valueChange:(UISwitch *)sw {
    if (self.item.switchChange) {
        self.item.switchChange(sw.isOn);
    };
}


//设置带开关的样式
- (void)setupMyCellItemTypeSwitch
{
    [self.contentView addSubview:self.aswitch];
}

//设置右图片
- (void) setRightImage
{
    
    self.rightImage = [[UIImageView alloc] initWithImage:self.item.itemRightImage];
    self.rightImage.centerY = self.contentView.centerY;
    switch (self.item.type) {
        case MyCellItemTypeSystem:
            self.rightImage.x = self.indicatorImage.x - DetailViewToIndicator - self.rightImage.width;
            break;
        case MyCellItemTypeSwitch:
            self.rightImage.x = self.aswitch.x - self.rightImage.width - DetailViewToIndicator;
            break;
        default:
            break;
    }
    
}

//设置右label
- (void)setRightItem
{
    self.detaillabel = [[UILabel alloc] init];
    self.detaillabel.text = self.item.itemRightname;
    self.detaillabel.font = [UIFont systemFontOfSize:14];
    self.detaillabel.size =  [self.detaillabel.text boundingRectWithSize:CGSizeMake(FLT_MAX, FLT_MAX) options:0 attributes:@{NSFontAttributeName :[UIFont systemFontOfSize:14]} context:nil].size;
    
    self.detaillabel.centerY = self.contentView.centerY;
    switch (self.item.type) {
        case MyCellItemTypeSystem:
            self.detaillabel.x = self.indicatorImage.x - DetailViewToIndicator - self.detaillabel.width;
            break;
         case MyCellItemTypeSwitch:
            self.detaillabel.x = self.aswitch.x - self.detaillabel.width - DetailViewToIndicator;
            break;
        default:
            break;
    }
    [self.contentView addSubview:self.detaillabel];
}





- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
